<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="180px" :rules="formRules">
                <el-form-item label="商品编号" prop="number">
                    <el-input v-model="formData.number" clearable placeholder="请输入商品编号" />
                </el-form-item>
                <el-form-item label="商品全名（管家婆）" prop="goodName">
                    <el-input v-model="formData.goodName" clearable placeholder="请输入商品全名（管家婆）" />
                </el-form-item>
                <el-form-item label="美菜商品全名" prop="mcName">
                    <el-input v-model="formData.mcName" clearable placeholder="请输入美菜商品全名" />
                </el-form-item>
                <el-form-item label="美菜规格" prop="mcUnit">
                    <el-input v-model="formData.mcUnit" clearable placeholder="请输入美菜规格" />
                </el-form-item>
                <el-form-item label="快驴商品全名" prop="klName">
                    <el-input v-model="formData.klName" clearable placeholder="请输入快驴商品全名" />
                </el-form-item>
                <el-form-item label="快驴规格" prop="klUnit">
                    <el-input v-model="formData.klUnit" clearable placeholder="请输入快驴规格" />
                </el-form-item>
                <el-form-item label="优大集全名" prop="ydjName">
                    <el-input v-model="formData.ydjName" clearable placeholder="请输入优大集商品全名" />
                </el-form-item>
                <el-form-item label="优大集规格" prop="ydjUnit">
                    <el-input v-model="formData.ydjUnit" clearable placeholder="请输入优大集规格" />
                </el-form-item>
                <el-form-item label="商品单位" prop="unit">
                    <el-input v-model="formData.unit" clearable placeholder="请输入商品单位" />
                </el-form-item>
                <el-form-item label="单价" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入单价" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="skuMappingEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSkuMappingAdd, apiSkuMappingEdit, apiSkuMappingDetail } from '@/api/sku_mapping'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑管家婆对照表' : '新增管家婆对照表'
})

// 表单数据
const formData = reactive({
    id: '',
    number: '',
    goodName: '',
    mcName: '',
    mcUnit: '',
    klName: '',
    klUnit: '',
    ydjName: '',
    ydjUnit: '',
    unit: '',
    price: '',
    isDelete: ''
})

// 表单验证
const formRules = reactive<any>({
    number: [
        {
            required: true,
            message: '请输入商品编号',
            trigger: ['blur']
        }
    ],
    goodName: [
        {
            required: true,
            message: '请输入商品全名（管家婆）',
            trigger: ['blur']
        }
    ],
    mcName: [
        {
            required: true,
            message: '请输入美菜商品全名',
            trigger: ['blur']
        }
    ],
    mcUnit: [
        {
            required: true,
            message: '请输入美菜规格',
            trigger: ['blur']
        }
    ],
    klName: [
        {
            required: true,
            message: '请输入快驴商品全名',
            trigger: ['blur']
        }
    ],
    klUnit: [
        {
            required: true,
            message: '请输入快驴规格',
            trigger: ['blur']
        }
    ],
    ydjName: [
        {
            required: true,
            message: '请输入优大集商品全名',
            trigger: ['blur']
        }
    ],
    ydjUnit: [
        {
            required: true,
            message: '请输入优大集规格',
            trigger: ['blur']
        }
    ],
    unit: [
        {
            required: true,
            message: '请输入商品单位',
            trigger: ['blur']
        }
    ],
    price: [
        {
            required: true,
            message: '请输入单价',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiSkuMappingDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiSkuMappingEdit(data) : await apiSkuMappingAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
